
#include <curl/curl.h>
#pragma comment(lib, "curllib.lib")

#include "peace_of_mind.h"

namespace pendina
{
	namespace secure
	{
		static const char *pCertFile = "testcert.pem";
		static const char *pCACertFile="cacert.pem";

		static size_t
		WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
		{
		  size_t realsize = size * nmemb;
		  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
		 
		  mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
		  if(mem->memory == NULL) {
			/* out of memory! */ 
			printf("not enough memory (realloc returned NULL)\n");
			return 0;
		  }
		  memcpy(&(mem->memory[mem->size]), contents, realsize);
		  mem->size += realsize;
		  mem->memory[mem->size] = 0;
		 
		  return realsize;
		}

		int peace_of_mind::fetch_url_content(char *rurl)
		{
			if(chunk.memory) {
				printf("freeing %lu ...\n", (long)chunk.size);
				free(chunk.memory);
			}
			chunk.memory = (char *)malloc(1);
			chunk.size = 0;    /* no data at this point */ 

			CURL *curl_handle;
			CURLcode res;
			curl_global_init(CURL_GLOBAL_ALL);
			curl_handle = curl_easy_init();
			curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
			curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);
			curl_easy_setopt(curl_handle, CURLOPT_URL, rurl);
			curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
			curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
			curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "pendina-trx-agent/1.0");
			res = curl_easy_perform(curl_handle);
			if(res == CURLE_OK) {
				//printf("%lu bytes retrieved\n", (long)chunk.size);
			}
			curl_easy_cleanup(curl_handle);
			curl_global_cleanup();
			return 0;
		}
		int peace_of_mind::post_file_content(char *rurl, char *phone, char *msg)
		{
			CURL *curl;
			CURLcode res;

			struct curl_httppost *formpost=NULL;
			struct curl_httppost *lastptr=NULL;
			struct curl_slist *headerlist=NULL;
			static const char buf[] = "Expect:";

			curl_global_init(CURL_GLOBAL_ALL);

			if(chunk.memory)
			{
				printf("freeing %lu ...\n", (long)chunk.size);
				free(chunk.memory);
			}
			chunk.memory = (char *)malloc(1);
			chunk.size = 0;    /* no data at this point */ 

			/* Fill in the filename field */ 
			curl_formadd(&formpost,
			&lastptr,
			CURLFORM_COPYNAME, "phone",
			CURLFORM_COPYCONTENTS, phone,
			CURLFORM_END);

			/* Fill in the filename field */ 
			curl_formadd(&formpost,
			&lastptr,
			CURLFORM_COPYNAME, "msg",
			CURLFORM_COPYCONTENTS, msg,
			CURLFORM_END);

			/* Fill in the submit field too, even if this is rarely needed */ 
			curl_formadd(&formpost,
			&lastptr,
			CURLFORM_COPYNAME, "submit",
			CURLFORM_COPYCONTENTS, "send",
			CURLFORM_END);

			curl = curl_easy_init();
			/* initalize custom header list (stating that Expect: 100-continue is not
			wanted */ 
			headerlist = curl_slist_append(headerlist, buf);
			if(curl) {
				/* what URL that receives this POST */ 
				curl_easy_setopt(curl, CURLOPT_URL, rurl);
				//if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
				/* only disable 100-continue header if explicitly requested */ 
				curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
				curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

				/* send all data to this function  */ 
				curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

				/* we pass our 'chunk' struct to the callback function */ 
				curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

				/* Perform the request, res will get the return code */ 
				res = curl_easy_perform(curl);
				/* Check for errors */ 
				if(res != CURLE_OK)
					fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

				/* always cleanup */ 
				curl_easy_cleanup(curl);

				/* then cleanup the formpost chain */ 
				curl_formfree(formpost);
				/* free slist */ 
				curl_slist_free_all (headerlist);
			}
			return 0;
		}
	}
}
